package com.sakura.controller;

import com.sakura.dto.InsertOrUpdateProjectDTO;
import com.sakura.dto.QueryCheckProjectDTO;
import com.sakura.dto.QueryManagerDTO;
import com.sakura.entity.CheckProject;
import com.sakura.entity.Manager;
import com.sakura.service.CheckProjectService;
import com.sakura.util.EntityConverter;
import com.sakura.util.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.sakura.util.R.getResult;

/**
 * @author : Sakura
 * @Date : 2023/11/23 15:31
 * @Description : TODO 体检项目
 */
@Api(value = "/api/project", tags = {"体检项目"})
@RestController
@RequestMapping("/api/project")
//解决跨域问题
@CrossOrigin
public class CheckProjectController {

    @Autowired
    private CheckProjectService checkProjectService;

    /**
     * @param projectDTO:
     * @return R
     * @author Sakura
     * @date 2023/11/23 15:32
     */
    @ApiOperation(value = "添加或者修改项目", notes = "", httpMethod = "POST")
    @PostMapping("/saveOrUpdate")
    public R addOrUpdate(@ApiParam(value = "新增或修改项目的数据传输对象", required = true) @RequestBody InsertOrUpdateProjectDTO projectDTO) {
        CheckProject checkProject = EntityConverter.copyAndGetSingle(projectDTO, CheckProject.class);
        boolean result = false;
        //为null或者为0就是新增
        if (projectDTO.getId() == null || projectDTO.getId() == 0) {
            setImageUrl(checkProject);
            result = checkProjectService.save(checkProject);
            return getResult(result);
        }
        //不为空则是修改
        setImageUrl(checkProject);
        result = checkProjectService.updateById(checkProject);
        return getResult(result);
    }

    @GetMapping("/query/{id}")
    @ApiOperation("通过id查找项目详情")
    public R getOne(@ApiParam(value = "项目id", required = true) @PathVariable Long id) {
        CheckProject project = checkProjectService.getById(id);
        return R.success(project);
    }

    @ApiOperation("通过删除项目")
    @DeleteMapping("/delete/{id}")
    public R deleteById(@PathVariable Long id) {
        boolean result = checkProjectService.removeById(id);
        return getResult(result);
    }

    @ApiOperation(value = "检查项目分页查询", notes = "检查项目分页查询")
    @PostMapping("/list")
    public R list(@ApiParam(type = "QueryManagerDTO" , value = "查询条件") @RequestBody QueryCheckProjectDTO queryCheckProjectDTO) {
        return R.success(checkProjectService.getPage(queryCheckProjectDTO));
    }

    private void setImageUrl(CheckProject checkProject) {
        if (checkProject.getImage() != null&&!checkProject.getImage().contains("https://")) {
            String prefix = "http://localhost:8081/api/image/download?name=";
            checkProject.setImage(prefix + checkProject.getImage());
        }
    }
    @ApiOperation(value = "根据集合删除项目", notes = "根据集合删除项目")
    @DeleteMapping("/batch")
    public R delete(@ApiParam(value = "项目集合id", required = true) @RequestBody List<Long> ids) {
        if (ids.size() == 0) {
            return R.error("请选择要删除的项目");
        }
        boolean result = checkProjectService.removeByIds(ids);
        return R.getResult(result);
    }
}
